//
//  SISSDatePickerView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/13.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSDatePickerView.h"

#define kDatePicHeight 216
#define kDateBtnHeight 45
#define SISSPickerViewHeight 216
@interface SISSDatePickerView ()

@property (nonatomic, weak) UIView *topView;
@property (nonatomic, weak) UIDatePicker *datePicker;
@property (nonatomic,strong) UILabel *titleLabel;

@property (nonatomic,assign) UIDatePickerMode type;

@property (nonatomic,strong) UIWindow *alertWindow;

@property (nonatomic,strong) UIView *alertView;

@property (nonatomic,strong) UIView *backgroundView;

@property (nonatomic,strong) UITapGestureRecognizer *tap;

@property (nonatomic,assign) BOOL hideTapHidden;

@end
@implementation SISSDatePickerView

//- (instancetype)initWithFrame:(CGRect)frame {
//    self =  [super initWithFrame:frame];
//    if (self) {
//        [self internalConfig];
//    }
//    return self;
//}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self internalConfig];
    }
    return self;
}

- (instancetype)initWithType:(UIDatePickerMode)type title:(NSString *)title{
    self = [super init];
    if (self) {
        self.type = type;
        self.title = title;
        [self internalConfig];
    }
    return self;
}


- (void)handleDatePickerAction:(NSDate *)date {
    if ([self.delegate respondsToSelector:@selector(changeTime:date:)]) {
        [self.delegate changeTime:self date:self.datePicker.date];
    }
}

- (void)setMinimumDate:(NSDate *)minimumDate {
    _minimumDate = minimumDate;
    self.datePicker.minimumDate = minimumDate;
}

- (void)setMaximumDate:(NSDate *)maximumDate {
    _maximumDate = maximumDate;
    self.datePicker.maximumDate = maximumDate;
}

- (void)internalConfig {
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.25];
    
    _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _alertWindow.windowLevel = UIWindowLevelAlert;
    self.frame = _alertWindow.bounds;
    
    //添加 底部遮罩
    _backgroundView = [[UIView alloc]initWithFrame:_alertWindow.bounds];
    
    _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.25];
    [self addSubview:_backgroundView];
    [self setUserInteractionEnabled:YES];
    [self setMultipleTouchEnabled:NO];
    [self.backgroundView setUserInteractionEnabled:YES];
    [self.backgroundView setMultipleTouchEnabled:NO];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    self.tap = tap;
    [_backgroundView addGestureRecognizer:tap];
    
    
    self.alertView = [UIView new];
    self.alertView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, kDateBtnHeight * 2 + kDatePicHeight);
    
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kDateBtnHeight, SCREEN_WIDTH, kDatePicHeight)];
    datePicker.datePickerMode = self.type;
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CHS_CN"];
    datePicker.backgroundColor = [UIColor whiteColor];
//    datePicker.minimumDate = [NSDate date];
    [datePicker addTarget:self action:@selector(handleDatePickerAction:) forControlEvents:UIControlEventValueChanged];
    self.datePicker = datePicker;
    [self.alertView addSubview:datePicker];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, datePicker.frame.origin.y - 43.5, self.frame.size.width, 43.5)];
    self.topView = topView;
    self.topView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 43.5, 43.5);
    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:button];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.text = self.title;
    [self.topView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topView);
    }];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(self.frame.size.width - 42, 0, 40, 43.5);
    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(completionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:button];
    
    [self.alertView addSubview:self.topView];
    
    [self showWithAnimation:YES];
}

- (void)showWithAnimation:(BOOL)animation{

    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundView.alpha = 1;
        CGRect frame = self.alertView.frame;
        frame.origin.y = SCREEN_HEIGHT - kDatePicHeight - kDateBtnHeight;
        self.alertView.frame = frame;
        
    }];
    
}
- (void)dismissWithAnimation:(BOOL)animation{
    
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.25];
        CGRect frame = self.alertView.frame;
        frame.origin.y = SCREEN_HEIGHT;
        self.alertView.frame = frame;
        
    }  completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setHideTapHidden:(BOOL)hideTapHidden{
    _hideTapHidden = hideTapHidden;
    if (hideTapHidden) {
        [_backgroundView removeGestureRecognizer:self.tap];
    }
}

- (void)setAlertView:(UIView *)alertView{
    _alertView = alertView;
    [self addSubview:alertView];
    _alertView.center = CGPointMake(_alertWindow.center.x, _alertWindow.center.y);
}

- (void)tap:(UITapGestureRecognizer *)tap{
    
    [self  dismissWithAnimation:NO];
}

- (void)completionButtonAction:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(determine:date:)]) {
        [self.delegate determine:self date:self.datePicker.date];
    }
    [self dismissWithAnimation:YES];
    
}

- (void)cancleButtonAction:(UIButton *)sender {
    [self dismissWithAnimation:YES];
}

- (void)setNowTime:(NSDate *)date {
    [self.datePicker setDate:date animated:YES];
}


// NSDate --> NSStringFw
- (NSString*)stringFromDate:(NSDate*)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    switch (self.type) {
        case UIDatePickerModeTime:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        case UIDatePickerModeDate:
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            break;
        case UIDatePickerModeDateAndTime:
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            break;
        case UIDatePickerModeCountDownTimer:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        default:
            break;
    }
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
    
}

//NSDate <-- NSString
- (NSDate*)dateFromString:(NSString*)dateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    switch (self.type) {
        case UIDatePickerModeTime:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        case UIDatePickerModeDate:
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            break;
        case UIDatePickerModeDateAndTime:
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            break;
        case UIDatePickerModeCountDownTimer:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        default:
            break;
    }
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    
    return destDate;
}

@end
